package com.dropdb.demo.controller.wx;

import com.dropdb.demo.bean.Captcha;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.config.AliyunComponentConfig;
import com.dropdb.demo.service.WxAuthService;
import com.dropdb.demo.service.WxHomeAndGoodsService;
import com.dropdb.demo.shiro.MallToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("wx/auth")
public class WxAuthController {

    @Autowired
    AliyunComponentConfig aliyunComponentConfig;

    @Autowired
    WxAuthService wxAuthService;

    @Autowired
    WxHomeAndGoodsService wxHomeAndGoodsService;

    static ConcurrentHashMap<String, Captcha> captchas = new ConcurrentHashMap<>();

    @PostMapping("regCaptcha")
    public BaseRespVO regCaptcha(@RequestBody Map map) {
        String mobile = (String) map.get("mobile");
        if (null == mobile) {
            return BaseRespVO.error(401, "非法参数");
        }
        if (!mobile.matches("^1[34578]\\d{9}$")) {
            return BaseRespVO.error(402, "手机号输入不正确");
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            stringBuilder.append((int)(Math.random() * 10));
        }
        //session.setAttribute("captcha", stringBuilder.toString());
        aliyunComponentConfig.sendMsg(mobile, stringBuilder.toString());
        captchas.put(mobile, new Captcha(stringBuilder.toString(), System.currentTimeMillis()));
        return BaseRespVO.ok();
    }

    @PostMapping("register")
    public BaseRespVO register(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        String mobile = (String) map.get("mobile");
        String code = (String) map.get("code");
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(code)) {
            return BaseRespVO.error(401, "注册信息不能为空");
        }
        if (wxAuthService.isMobileExisted(mobile)) {
            return BaseRespVO.error(705, "手机号已被注册");
        }
        //String captcha = (String) session.getAttribute("captcha");
        Captcha captcha = captchas.get(mobile);
        if (null == captcha) { //没有获取验证码
            return BaseRespVO.error(402, "验证码错误");
        }
        if (System.currentTimeMillis() - captcha.getCurrentTime() > 300000) { //验证码超时
            captchas.remove(mobile);
            return BaseRespVO.error(402, "验证码错误");
        }
        if (!code.equals(captcha.getCaptcha())) { //验证不匹配
            return BaseRespVO.error(402, "验证码错误");
        }
        int register = wxAuthService.register(username, password, mobile);
        if (1 != register) {
            captchas.remove(mobile);
            return BaseRespVO.error(704, "用户名已注册");
        }
        captchas.remove(mobile);
        //登录
        MallToken mallToken = new MallToken(username, password, "wx");
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(mallToken);
        } catch (AuthenticationException e) {
            //e.printStackTrace();
            return BaseRespVO.error(402,"参数值不对");
        }
        Map<String,Object> vo = wxHomeAndGoodsService.queryUser(username);
        return BaseRespVO.ok(vo);
    }

    @PostMapping("reset")
    public BaseRespVO reset(@RequestBody Map map) {
        String mobile = (String) map.get("mobile");
        String code = (String) map.get("code");
        String password = (String) map.get("password");
        if (StringUtils.isEmpty(password) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(code)) {
            return BaseRespVO.error(401, "注册信息不能为空");
        }
        Captcha captcha = captchas.get(mobile);
        if (null == captcha) { //没有获取验证码
            return BaseRespVO.error(402, "验证码错误");
        }
        if (System.currentTimeMillis() - captcha.getCurrentTime() > 300000) { //验证码超时
            captchas.remove(mobile);
            return BaseRespVO.error(402, "验证码错误");
        }
        if (!code.equals(captcha.getCaptcha())) { //验证不匹配
            return BaseRespVO.error(402, "验证码错误");
        }
        int i = wxAuthService.reset(mobile, password);
        return 1 == i ? BaseRespVO.ok() : BaseRespVO.error(706, "手机号未注册");
    }



}
